package com.guguskins.back.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.guguskins.back.service.GuOrderService;
import com.guguskins.back.service.GuWithdrawalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author duttonliu
 * @date 2021/7/21 9:11
 */
@RestController
@RequestMapping("/thirdCallback")
@Slf4j
public class ThirdCallbackController {

    @Autowired
    private GuOrderService orderService;

    @Autowired
    private GuWithdrawalService withdrawalService;

    @PostMapping("/unionpay")
    public String unionpayCallback(@RequestParam Map<String, Object> map) {

        log.info("银联回调信息：{}", JSON.toJSONString(map));

        String billNo = map.get("billNo").toString();
        String billStatus = map.get("billStatus").toString();
        //修改订单状态
        orderService.callback(billNo, billStatus, JSON.toJSONString(map));

        return "SUCCESS";
    }

    @PostMapping("/steamBot")
    public JSONObject steamBotCallback(@RequestBody JSONObject jsonObject) {

        if (withdrawalService.callback(jsonObject)) {
            return new JSONObject() {
                {
                    put("code", 0);
                    put("body", new JSONObject());
                    put("message", "ok");
                }
            };
        }
        return new JSONObject() {
            {
                put("code", -1);
                put("body", new JSONObject());
                put("message", "fail");
            }
        };
    }
}
